package com.wyl.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.wyl.template.domain.common.PageQuery;
import com.wyl.template.domain.common.ResponseResult;
import com.wyl.template.util.ParamUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * @Auther: wyl
 * @Date: 2021/6/22
 * @Description:
 */
public class BaseController<M extends IService<T>, T> {
    @Autowired
    private M iService;

    public ResponseResult<IPage<T>> selectPage(PageQuery query) {
        Type type = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
        QueryWrapper<T> queryWrapper = ParamUtils.getParamWrapper(type.getTypeName(), query.getParam());
        Page<T> pageable = ParamUtils.getPage(query.getCurrent(), query.getPageSize(), query.getSortParam(), query.getSortDirection(), query.isOrder());
        return ResponseResult.success(iService.page(pageable, queryWrapper));
    }
}
